﻿using UnityEngine;
using System.Collections;

public class CycleSounds : MonoBehaviour {
	
	//public variables for the audio clips
	public AudioClip OneVoice;
	public AudioClip ChickenClip;
	public AudioClip ChickenCash;
	
	//the second option
	public AudioClip TwoVoice;
	public AudioClip ChineseClip;
	public AudioClip ChineseCash;
	
	//the third option
	public AudioClip ThreeVoice;
	public AudioClip CoffeeClip;
	public AudioClip CoffeeCash;
	
	//the fourth option
	public AudioClip FourVoice;
	public AudioClip GrillClip;
	public AudioClip GrillCash;
	
	//the final option
	public AudioClip FiveVoice;
	public AudioClip SeafoodClip;
	public AudioClip SeafoodCash;
	
	//a counter
	int counter, shortCounter;
	
	//a boolean to check if the cycle should be working
	bool isCycling, shortCycle;
	
	// Use this for initialization
	void Start () {
		//set the counter to be 1
		counter = 1;
		//set the short counter to be 0
		shortCounter = 0;
		//set the bool to be true
		isCycling = false;
		//set the short cycle to be false
		shortCycle = false;
	}
	
	// Update is called once per frame
	void Update () {
		//check to see if the enter key is pressed then chnage the cycle stats
		if(Input.GetKeyDown(KeyCode.Return)){
			counter = 1;
			shortCycle = false;
		}
		
		//if the first option is repeated
		if(Input.GetKeyDown(KeyCode.F1)) {
			
			//set the short counter to be 0
			shortCounter = 0;
			//set the actual counter to the correct position
			counter = 1;
			//enable the shortcycle boolean
			shortCycle = true;
			//disable the isCycling
			isCycling = false;
		}
		
		//if the second option is repeated
		if(Input.GetKeyDown(KeyCode.F2)) {
			
			//set the short counter to be 0
			shortCounter = 0;
			//set the actual counter to the correct position
			counter = 4;
			//enable the shortcycle boolean
			shortCycle = true;
			//disable the isCycling
			isCycling = false;
		}
		
		//if the third option is repeated
		if(Input.GetKeyDown(KeyCode.F3)) {
			
			//set the short counter to be 0
			shortCounter = 0;
			//set the actual counter to the correct position
			counter = 7;
			//enable the shortcycle boolean
			shortCycle = true;
			//disable the isCycling
			isCycling = false;
		}
		
		//if the fourth option is repeated
		if(Input.GetKeyDown(KeyCode.F4)) {
			
			//set the short counter to be 0
			shortCounter = 0;
			//set the actual counter to the correct position
			counter = 10;
			//enable the shortcycle boolean
			shortCycle = true;
			//disable the isCycling
			isCycling = false;
		}
		
		//if the fifth option is repeated
		if(Input.GetKeyDown(KeyCode.F5)) {
			
			//set the short counter to be 0
			shortCounter = 0;
			//set the actual counter to the correct position
			counter = 13;
			//enable the shortcycle boolean
			shortCycle = true;
			//disable the isCycling
			isCycling = false;
		}
		
		//make sure a sound isn't currently playing
		if(!audio.isPlaying && isCycling == true) {
			//check to see if the counter has exceeded
			if((counter % 16) != 0) {
				//set the audio clip and play it
				audio.clip = checkType();
				//Debug.Log(counter);
				audio.Play();
				//add to the counter
				counter++;
			}
			//set the counter back to zero
			else {
				counter = 1;
				isCycling = false;
			}
		}
		//check to see if the audio isn't playing and if the short cycle is true
		if(!audio.isPlaying && shortCycle == true) {
			//check to see if the cycle counter hasn't exceeded three
			if(shortCounter < 3) {
				//cycle through the sounds based on the counter info
				//set the audio clip and play it
				audio.clip = checkType();
				//Debug.Log(counter);
				audio.Play();
				//add to the counter
				counter++;
				//add to the short counter
				shortCounter++;
			}
			//set the short cycle to be false if the counter is done
			else {
				shortCycle = false;
			}
		}
		
	}
	
	//returns the current clip for the restaurant riff
	private AudioClip checkType() {
		//set up a switch statement to see what audio clip should be returned
		switch(counter) {
		case 1:
			return OneVoice;
		case 2:
			return ChickenClip;
		case 3:
			return ChickenCash;
		case 4:
			return TwoVoice;
		case 5:
			return ChineseClip;
		case 6:
			return ChineseCash;
		case 7:
			return ThreeVoice;
		case 8:
			return CoffeeClip;
		case 9:
			return CoffeeCash;
		case 10:
			return FourVoice;
		case 11:
			return GrillClip;
		case 12:
			return GrillCash;
		case 13: 
			return FiveVoice;
		case 14:
			return SeafoodClip;
		case 15:
			return SeafoodCash;
			
		}
		return null;
	}
	
	//takes in the key and performs the appropriate action
	public bool keyOper(bool toggle) {
		if(toggle == true) {
			return isCycling = true;
		}
		else {
			return isCycling = false;
			counter = 1;
		}
	}
	
	public bool getCycling(){
		return isCycling;
	}
	
	
	
	
	
	
	
	
	
	
}